package com.solution.dao;


import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;

import com.solution.exception.DAOException;
import com.solution.model.PO;
import com.solution.model.UserRole;

/**
 * Exposes the CRUD methods for the User Role table.
 *
 */
public class UserRoleDAOImpl  extends DAOImpl<UserRole> implements UserRoleDAO {
	
	private Logger log = Logger.getLogger(UserTypeDAOImpl.class.getName());
	
	@Override
	protected Class<? extends PO> getPOClass() {
		return UserRole.class;
	}

	/**
	 * Delete all user role entry for given user
	 * @param userID
	 * @return number of roles deleted
	 * @throws DAOException
	 */	
	public int deleteAllRoleForUser(Long userID) throws DAOException {
		log.debug("Entering :: UserRoleDAOImpl:getUserRoleList()");
		try{
			String hql = "delete UserRole where User_InfoID = :userId";
	        Query query = getCurrentSession().createQuery(hql);
	        query.setLong("userId",userID);                
	        int rowCount= query.executeUpdate();
	        flushSession();
	        return rowCount;
		}
		catch(HibernateException he){
			log.error(he, he);
			throw new DAOException(he);
		}
	}
}
